import { AppModel } from '../../../../model/AppModel'
import { createGPIOMenu } from '../BlockPackage'


export default {

    category: Blockly.Categories.bepart ,
    
    color: [
        Blockly.Colours.bepart.primary,
        Blockly.Colours.bepart.secondary,
        Blockly.Colours.bepart.tertiary
    ] ,

    previousStatement: true ,
    nextStatement: true ,

    init(model: AppModel) {
        this.appendDummyInput()
            .appendField("设置")
            .appendField(new Blockly.FieldDropdown(async () => {
                return [
                    ["串口 1", 'Serial1'] ,
                    ["串口 2", 'Serial2'] ,
                    ["串口 3", 'Serial3'] ,
                    // ["I2C 1", 'I2C1'] ,
                    // ["I2C 2", 'I2C2'] ,
                    // ["SPI 1", 'SPI1'] ,
                    // ["SPI 2", 'SPI2'] ,
                ]
            }), 'device')

        this.appendDummyInput()                
            .appendField("引脚 TX:")
            .appendField(createGPIOMenu(model, true), 'tx')
            .appendField("RX:")
            .appendField(createGPIOMenu(model), 'rx')
            .appendField("波特率:")
            .appendField(new Blockly.FieldDropdown(async () => {
                return [
                    ["9600", "9600"] ,
                    ["57600", "57600"] ,
                    ["115200", "115200"] ,
                    ["250000", "250000"] ,
                    ["500000", "500000"] ,
                    ["1000000", "1000000"] ,
                    ["2000000", "2000000"] ,
                    ["5000000", "5000000"] ,
                ]
            }), 'baudrate')

    } ,

    run (args: any, util: any, runtime: any) {
    } ,

    coder: {
        JavaScript(block: any) {
            
            let device = block.getFieldValue('device')
            let pinTX = block.getFieldValue('tx')
            let pinRX = block.getFieldValue('rx')
            let baudrate = block.getFieldValue('baudrate')

            return `${device}.setup(${baudrate}, {tx:${pinTX}, rx:${pinRX}})\n`
        }
    }
}
